package bing.search.es;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

import bing.search.common.Document;
import bing.search.common.IndexTypeField;
import bing.search.hbase.HbaseUtils;

@Service
public class IndexService {

	public static final String INDEX_NAME="tfjt";
	public static final String INDEX_TYPE="docs";

	public void createIndex(String indexName, String shardsNum, String replicasNum) throws Exception {
		EsUtils.createIndex(indexName, shardsNum, replicasNum);
	}
	
	public void createIndexType(String indexName, String typeName, List<IndexTypeField> settings) throws IOException {
		EsUtils.createIndexType(indexName, typeName, settings);
	}
	
	public void createDocumentIndex() throws Exception {
		EsUtils.createDocumentIndex(INDEX_NAME, INDEX_TYPE);
	}
	
	public List<String> loadAllIndices(){
		return EsUtils.loadAllIndices();
	}
	
	public String loadIndexByName(String indexName) throws IOException{
		return EsUtils.loadIndexByName(indexName);
	}
	
	public Map<String,Object> searchIndexByName(String indexName, int start, int size){
		return EsUtils.searchIndexByName(indexName, start, size);
	}
	
	public boolean deleteIndexByName(String indexName){
		return EsUtils.deleteIndexByName(indexName);
	}
	//
	public void addIndex() throws IOException {
		List<Document> docList=Lists.newArrayList();
		File file=new File("C:\\Users\\trs\\git\\hbase-elasticsearch-example\\doc1.txt");
		List<String> lines=FileUtils.readLines(file, "UTF8");
		
		for(String line:lines) {
			Document doc=new Document();
			String[] split=line.split("\t");
			System.out.println(split[0]);
			
			int docId=Integer.parseInt(split[0].trim());
			doc.setId(docId);
			doc.setTitle(split[1]);
			doc.setAuthor(split[2]);
			doc.setDescribe(split[3]);
			doc.setContent(split[3]);
			
			docList.add(doc);
		}
		System.out.println("docList size: "+docList.size());
		HbaseUtils hbaseUtils=new HbaseUtils();
		for(Document doc : docList) {
			//把数据写入hbase
			//TableName tableName, String row, String family, String column, String data
			hbaseUtils.put(hbaseUtils.DOCUMENT_TABLE_NAME, String.valueOf(doc.getId()), hbaseUtils.DOCUMENT_TABLE_CF_1, hbaseUtils.COLUMNFAMILY_1_TITLE, doc.getTitle());
			hbaseUtils.put(hbaseUtils.DOCUMENT_TABLE_NAME, String.valueOf(doc.getId()), hbaseUtils.DOCUMENT_TABLE_CF_1, hbaseUtils.COLUMNFAMILY_1_AUTHOR, doc.getAuthor());
			hbaseUtils.put(hbaseUtils.DOCUMENT_TABLE_NAME, String.valueOf(doc.getId()), hbaseUtils.DOCUMENT_TABLE_CF_1, hbaseUtils.COLUMNFAMILY_1_DESCRIBE, doc.getDescribe());
			hbaseUtils.put(hbaseUtils.DOCUMENT_TABLE_NAME, String.valueOf(doc.getId()), hbaseUtils.DOCUMENT_TABLE_CF_1, hbaseUtils.COLUMNFAMILY_1_CONTENT, doc.getContent());
			
			//把数据写入es,保存索引
			System.out.println(EsUtils.addIndex(INDEX_NAME, INDEX_TYPE, doc));
		}
	}
	
	public void insert(String dataPath,String indexPath) throws IOException {
		List<Document> docList=Lists.newArrayList();
		File file=new File(dataPath);
		List<String> lines=FileUtils.readLines(file, "UTF8");
		
		for(String line:lines) {
			Document doc=new Document();
			String[] split=line.split("\t");
			System.out.println(split[0]);
			
			int docId=Integer.parseInt(split[0].trim());
			doc.setId(docId);
			doc.setTitle(split[1]);
			doc.setAuthor(split[2]);
			doc.setDescribe(split[3]);
			doc.setContent(split[3]);
		}
		HbaseUtils hbaseUtils=new HbaseUtils();
		for(Document doc : docList) {
			//把数据写入hbase
			//TableName tableName, String row, String family, String column, String data
			hbaseUtils.put(hbaseUtils.DOCUMENT_TABLE_NAME, String.valueOf(doc.getId()), hbaseUtils.DOCUMENT_TABLE_CF_1, hbaseUtils.COLUMNFAMILY_1_TITLE, doc.getTitle());
			hbaseUtils.put(hbaseUtils.DOCUMENT_TABLE_NAME, String.valueOf(doc.getId()), hbaseUtils.DOCUMENT_TABLE_CF_1, hbaseUtils.COLUMNFAMILY_1_AUTHOR, doc.getAuthor());
			hbaseUtils.put(hbaseUtils.DOCUMENT_TABLE_NAME, String.valueOf(doc.getId()), hbaseUtils.DOCUMENT_TABLE_CF_1, hbaseUtils.COLUMNFAMILY_1_DESCRIBE, doc.getDescribe());
			hbaseUtils.put(hbaseUtils.DOCUMENT_TABLE_NAME, String.valueOf(doc.getId()), hbaseUtils.DOCUMENT_TABLE_CF_1, hbaseUtils.COLUMNFAMILY_1_CONTENT, doc.getContent());
			
			//把数据写入es,保存索引
			EsUtils.addIndex(INDEX_NAME, INDEX_TYPE, doc);
		}
	}

}
